using WpfClient.Core.Models;

namespace WpfClient.Core.Communication
{
    /// <summary>
    /// Socket服务器接口
    /// </summary>
    public interface ISocketServer
    {
        /// <summary>
        /// 服务器是否正在运行
        /// </summary>
        bool IsRunning { get; }

        /// <summary>
        /// 当前连接的客户端数量
        /// </summary>
        int ConnectedClientsCount { get; }

        /// <summary>
        /// 客户端连接事件
        /// </summary>
        event EventHandler<ClientConnectedEventArgs> ClientConnected;

        /// <summary>
        /// 客户端断开连接事件
        /// </summary>
        event EventHandler<ClientDisconnectedEventArgs> ClientDisconnected;

        /// <summary>
        /// 数据发送完成事件
        /// </summary>
        event EventHandler<DataSentEventArgs> DataSent;

        /// <summary>
        /// 接收到数据事件
        /// </summary>
        event EventHandler<DataReceivedEventArgs> DataReceived;

        /// <summary>
        /// 启动服务器
        /// </summary>
        /// <param name="port">监听端口</param>
        /// <param name="cancellationToken">取消令牌</param>
        Task StartAsync(int port, CancellationToken cancellationToken = default);

        /// <summary>
        /// 停止服务器
        /// </summary>
        Task StopAsync();

        /// <summary>
        /// 向指定客户端发送数据
        /// </summary>
        /// <param name="clientId">客户端ID</param>
        /// <param name="data">要发送的数据</param>
        /// <param name="expectedResponseHeader">期望的回复头部</param>
        /// <param name="timeout">超时时间</param>
        Task<bool> SendDataAsync(string clientId, byte[] data, byte[] expectedResponseHeader, TimeSpan timeout = default);

        /// <summary>
        /// 向所有客户端发送数据
        /// </summary>
        /// <param name="data">要发送的数据</param>
        /// <param name="expectedResponseHeader">期望的回复头部</param>
        /// <param name="timeout">超时时间</param>
        Task<Dictionary<string, bool>> SendDataToAllAsync(byte[] data, byte[] expectedResponseHeader, TimeSpan timeout = default);

        /// <summary>
        /// 获取所有连接的客户端信息
        /// </summary>
        Task<IEnumerable<ClientInfo>> GetConnectedClientsAsync();
    }
}